package com.global.college.ui.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.global.college.R;
import com.global.college.adapter.TrainExpandAdapter;
import com.global.college.api.ProgressSubscriber;
import com.global.college.api.RequestClient;
import com.global.college.base.BaseFragment;
import com.global.college.data.net.TrainStageBean;
import com.global.college.ui.activity.TrainDetailBoughtActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * 训练营-目录
 */
public class TrainDetailFragment extends BaseFragment {
    @BindView(R.id.recycler_train)
    RecyclerView recyclerTrain;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String trainId;
    private String mParam2;

    private TrainExpandAdapter trainExpandAdapter;

    public TrainDetailFragment() {
    }

    public static TrainDetailFragment newInstance(String param1, String param2) {
        TrainDetailFragment fragment = new TrainDetailFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            trainId = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        Log.i(TAG, "trainId:" + trainId);
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_study_path;
    }

    @Override
    public void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        recyclerTrain.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    @Override
    protected void initData() {
        getStudyPath();
    }

    private void getStudyPath() {
        RequestClient.getInstance().getTrainList(trainId).subscribe(new ProgressSubscriber<TrainStageBean[]>(mActivity, false) {
            @Override
            public void onNext(TrainStageBean[] model) {
                List<TrainStageBean> stageBeanList = Arrays.asList(model);
                if (stageBeanList.get(0).getTrainingClassList() != null && stageBeanList.get(0).getTrainingClassList().size() > 0) {
                    //三层
                    setTrainThreeAdapter(stageBeanList);
                    ((TrainDetailBoughtActivity) mActivity).setDefaultClassId(stageBeanList.get(0).getTrainingClassList().get(0).getClassModelList().get(0).getClassId());
                } else {
                    //两层
                    setTrainTwoAdapter(stageBeanList);
                    ((TrainDetailBoughtActivity) mActivity).setDefaultClassId(stageBeanList.get(0).getClassModelList().get(0).getClassId());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    private void setTrainTwoAdapter(List<TrainStageBean> stageBeanList) {
        List data = new ArrayList<>();
        for (TrainStageBean stageBean : stageBeanList) {
            for (TrainStageBean.TrainWeekBean.ClassModelListBean classBean : stageBean.getClassModelList()) {
                stageBean.addSubItem(classBean);
            }
            data.add(stageBean);
        }
        trainExpandAdapter = new TrainExpandAdapter(data, trainId);
        trainExpandAdapter.bindToRecyclerView(recyclerTrain);

    }

    private void setTrainThreeAdapter(List<TrainStageBean> stageBeanList) {
        List data = new ArrayList<>();
        for (TrainStageBean stageBean : stageBeanList) {
            for (TrainStageBean.TrainWeekBean weekBean : stageBean.getTrainingClassList()) {
                for (TrainStageBean.TrainWeekBean.ClassModelListBean classBean : weekBean.getClassModelList()) {
                    weekBean.addSubItem(classBean);
                }
                stageBean.addSubItem(weekBean);
            }
            data.add(stageBean);
        }
        trainExpandAdapter = new TrainExpandAdapter(data, trainId);
        trainExpandAdapter.bindToRecyclerView(recyclerTrain);
    }

}
